package chuangyi.com.org.ViewModel;

import android.content.Context;
import android.databinding.Bindable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.annotation.LayoutRes;
import android.view.View;

import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;

/**
 * Created by zxh on 16/12/9.
 */

public class AdviceItem extends BindingListItem {
    private int type;
    private int uid;
    private boolean readed;
    private String content;
    private Date uploadDate;
    private String nickName;
    private String avatar;
    private String job;
    private String times;
    private AdviceInfo parent;

    @Bindable
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Bindable
    public String getTimes() {
        return times;
    }

    public void setTimes(String times) {
        this.times = times;
        notifyPropertyChanged(BR.times);
    }

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public void setParent(AdviceInfo parent) {
        this.parent = parent;
    }

    @Bindable
    public boolean isReaded() {
        return readed;
    }

    public void setReaded(boolean readed) {
        this.readed = readed;
    }

    @Bindable
    public Date getUploadDate() {
        return uploadDate;
    }

    public void setUploadDate(Date uploadDate) {
        this.uploadDate = uploadDate;
    }

    public boolean isMine() {
        return uid == User.getCurrentUser().getUid();
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
        updateLayout();
    }

    @Bindable
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Bindable
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
        updateLayout();
    }

    public AdviceItem(Context context) {
        super(context, 0);
    }

    private void updateLayout() {
        if (this.type == AdviceInfo.VOICE_TYPE) {
            if (isMine()) {
                setLayout(R.layout.i_advice_mine_voice);
            } else {
                setLayout(R.layout.i_advice_their_voice);
            }
        } else if (this.type == AdviceInfo.TEXT_TYPE) {
            if (isMine()) {
                setLayout(R.layout.i_advice_mine_text);
            } else {
                setLayout(R.layout.i_advice_their_text);
            }
        }
    }

    /**
     * 点击项
     *
     * @param view
     */
    public void onClickItem(View view) {
        if (type == AdviceInfo.VOICE_TYPE) {
            if (isMine() == false && parent.getCharge() > 0) {
                if (!parent.isPaid()) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            synchronized (parent) {
                                final boolean allowPlay = parent.allowPlay();
                                parent.setPaid(allowPlay);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (allowPlay) {
                                            parent.playSound(content);
                                        } else {
                                            parent.showPayActivity();
                                        }

                                    }
                                });
                            }
                        }
                    }).start();
                    return;
                }
            }
            parent.playSound(content);
            // 判断是否已付款，已付款时直接播放声音，未付款则跳转到支付页
        }
    }
}
